#include "initArc.h"
#include "ui_initArc.h"

initArc::initArc(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::initArc)
{
    ui->setupUi(this);

    arcFile.setFileName(arcFilePath);

    connect(ui->radioBtn_bike, &QRadioButton::clicked, [=](){
        bikePass = true;
    });
    connect(ui->radioBtn_walk, &QRadioButton::clicked, [=](){
        bikePass = false;
    });
    connect(ui->btn_save, &QPushButton::clicked, [=](){
        SaveArc();
        this->close();
        Init();
    });
}

initArc::~initArc()
{
    delete ui;
}

void initArc::GetArc(int x1, int y1, int x2, int y2, int campus, myGraph *graph)
{
    this->graph = graph;
    point1 = DeterPoint(x1, y1, campus);
    point2 = DeterPoint(x2, y2, campus);
    ui->lbl_point1->setText("点1：" + graph->point[point1].pointName.first());
    ui->lbl_point2->setText("点2：" + graph->point[point2].pointName.first());

    scale = campus ? scaleShaHe : scaleXiTuCheng;
    distance = qSqrt(qPow(graph->point[point1].pointX - graph->point[point2].pointX, 2) +
                     qPow(graph->point[point1].pointY - graph->point[point2].pointY, 2))
            * scale;
    ui->line_distance->setText(QString::number(distance));
}

int initArc::DeterPoint(int x, int y, int campus)
{
    int disX = 100, disY = 100;
    int absX, absY;
    int point;

    int max = campus ? graph->pointNumShaHe : graph->pointNum;
    for(int i = campus ? 1 : graph->pointNumShaHe + 1; i <= max; i++)
    {
        absX = qAbs(graph->point[i].pointX - x);
        absY = qAbs(graph->point[i].pointY - y);
        if(absX + absY < disX + disY)
        {
            disX = absX;
            disY = absY;
            point = i;
        }
    }
    return point;
}
void initArc::SaveArc()
{
    int i, max;

    if( ! ui->line_peakPeriod->text().isEmpty())
    {
        switch (ui->line_peakPeriod->text().toInt())
        {
        case(0)://课间
            peakPeriod += "7:45-8:00/"
                          "9:35-9:50/"
                          "14:35-14:45/"
                          "15:30-15:40/";
            peakValue  += "2/"
                          "2/"
                          "2/"
                          "2/";
        case(1)://早中晚
            peakPeriod += "12:15-13:00/"
                          "18:10-18:30";
            peakValue  += "2.5/"
                          "2.5";
            break;
        }
    }

    max = ui->line_peakPeriod->text().isEmpty() ? 0 : peakPeriod.split("/").size();
    for(i = 0; i < max; i ++)
    {
        if(i == 0)
            peakValue += "2";
        else
            peakValue += "/2";
    }
    if( ! arcFile.open(QIODevice::Append))
        qDebug() << "打开边文件错误";
    QTextStream stream(&arcFile);
    QString arc = QString::number(point1) + " " +
            QString::number(point2) + " " +
            QString::number(distance) + " " +
            QString::number(bikePass) + " " +
            peakPeriod + " " +
            peakValue;
    stream << arc << "\r\n";
    arcFile.close();
}
void initArc::Init(void)
{
    peakPeriod.clear();
    peakValue.clear();
    ui->line_distance->clear();
    ui->radioBtn_bike->setChecked(true);
    bikePass = true;
    ui->line_peakPeriod->clear();
}
